﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using ANGFLib;

namespace ANGF
{
    public partial class FormEditComponentList : Form
    {
        public enum EditComponentType { Place, Person };
        private MyXmlDoc doc;
        private EditComponentType componentType;
        private Type subUI;

        private string componentNameToElementName(EditComponentType componentType)
        {
            switch (componentType)
            {
                case EditComponentType.Place: return "Place";
                case EditComponentType.Person: return "Person";
            }
            return null;
        }

        public FormEditComponentList(MyXmlDoc doc, EditComponentType componentType, Type subUI)
        {
            this.componentType = componentType;
            this.subUI = subUI;
            this.doc = doc;
            InitializeComponent();
        }

        private void FormEditComponentList_Load(object sender, EventArgs e)
        {
            XNamespace ns = ANGFLib.XmlNamespacesConstants.StdXmlNamespace;
            this.Text = componentType.ToString() + "編集";
            updateList();


        }
        private string createItemString(int index)
        {
            XNamespace ns = ANGFLib.XmlNamespacesConstants.StdXmlNamespace;
            if (index < 0) return "";
            XElement n = (XElement)this.listBox1.Items[index];
            if (n == null) return "";
            return n.Element(ns + "Name").Value + " (" + n.Element(ns + "Id").Value + ")";
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            buttonEdit.Enabled = listBox1.SelectedIndex >= 0;
            buttonRemove.Enabled = listBox1.SelectedIndex >= 0;
        }

        private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            string s = createItemString(e.Index);
            e.Graphics.DrawString(s, e.Font, new SolidBrush(e.ForeColor), e.Bounds);
            e.DrawFocusRectangle();
        }

        private void updateList()
        {
            XNamespace ns = ANGFLib.XmlNamespacesConstants.StdXmlNamespace;
            this.listBox1.Items.Clear();
            foreach (var n in doc.moduleEx.Elements(ns + componentNameToElementName(componentType)))
            {
                this.listBox1.Items.Add(n);
            }

        }
        private void buttonNew_Click(object sender, EventArgs e)
        {
            var form = (Form)Activator.CreateInstance(subUI, doc, null);
            if (form.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                updateList();
                XNamespace ns = ANGFLib.XmlNamespacesConstants.StdXmlNamespace;
                for (int i = 0; i < listBox1.Items.Count; i++)
                {
                    if ((string)form.Tag == ((XElement)listBox1.Items[i]).Element(ns + "Id").Value)
                    {
                        this.listBox1.SelectedIndex = i;
                        break;
                    }
                }
            }
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedIndex < 0) return;
            var form = (Form)Activator.CreateInstance(subUI, doc, (XElement)this.listBox1.Items[this.listBox1.SelectedIndex]);
            if (form.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                updateList();
                XNamespace ns = ANGFLib.XmlNamespacesConstants.StdXmlNamespace;
                for (int i = 0; i < listBox1.Items.Count; i++)
                {
                    if ((string)form.Tag == ((XElement)listBox1.Items[i]).Element(ns + "Id").Value)
                    {
                        this.listBox1.SelectedIndex = i;
                        break;
                    }
                }
            }
        }

        private void buttonRemove_Click(object sender, EventArgs e)
        {
            XNamespace ns = ANGFLib.XmlNamespacesConstants.StdXmlNamespace;
            if (this.listBox1.SelectedIndex < 0) return;
            var targetElement = (XElement)this.listBox1.Items[this.listBox1.SelectedIndex];
            DialogResult result = MessageBox.Show(this, targetElement.Element(ns+"Name").Value + "を本当に削除しますか?", this.Text, MessageBoxButtons.YesNo);
            if (result != System.Windows.Forms.DialogResult.Yes) return;
            targetElement.Remove();
            int index = listBox1.SelectedIndex;
            updateList();
            listBox1.SelectedIndex = Math.Min(listBox1.Items.Count - 1, index);
        }
    }
}
